package com.taomeishu.mailcrazy.tb.bo;

import java.util.Collection;
import java.util.List;

import wint.lang.utils.CollectionUtil;
import wint.lang.utils.StringUtil;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.ItemSearch;
import com.taobao.api.domain.TradeRate;
import com.taobao.api.domain.User;
import com.taobao.api.request.ItemcatsGetRequest;
import com.taobao.api.request.ItemsSearchRequest;
import com.taobao.api.request.TraderatesSearchRequest;
import com.taobao.api.request.UserGetRequest;
import com.taobao.api.request.UsersGetRequest;
import com.taobao.api.response.ItemcatsGetResponse;
import com.taobao.api.response.ItemsSearchResponse;
import com.taobao.api.response.TraderatesSearchResponse;
import com.taobao.api.response.UserGetResponse;
import com.taobao.api.response.UsersGetResponse;
import com.taomeishu.mailcrazy.tb.util.SearchResult;

public class TbOpenBOImpl implements TbOpenBO {

	private String url;

	private String appkey;

	private String secret;

	private long searchProductPageSize = 100;

	@Override
	public List<ItemCat> getItemCatByParentCid(Long parentCid) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			ItemcatsGetRequest req = new ItemcatsGetRequest();
			req.setFields("cid,parent_cid,name,is_parent");
			req.setParentCid(parentCid);
			ItemcatsGetResponse response = client.execute(req);
			if (!response.isSuccess()) {
				return CollectionUtil.newArrayList();
			}
			return response.getItemCats();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User getPrivateUser(String nick) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			UserGetRequest req = new UserGetRequest();
			req.setFields("user_id,nick,sex,buyer_credit,seller_credit,location,birthday,type,alipay_account,has_shop,vip_info,email");
			req.setNick(nick);
			UserGetResponse response = client.execute(req);
			return response.getUser();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<User> getPublicUsers(Collection<String> nicks) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			UsersGetRequest req = new UsersGetRequest();
			req.setFields("user_id,nick,sex,buyer_credit,seller_credit,location,birthday,type,alipay_account,has_shop,vip_info,email");
			String nicksString = CollectionUtil.join(CollectionUtil.newArrayList(nicks), ",");
			req.setNicks(nicksString);
			UsersGetResponse response = client.execute(req);
			return response.getUsers();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Item> searchItems(long cid, long page) {
		return searchItems(cid, null, page);
	}

	private List<Item> searchItems(long cid, String q, long page) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			ItemsSearchRequest req = new ItemsSearchRequest();
			req.setCid(cid);
			req.setPageSize(searchProductPageSize);
			if (!StringUtil.isEmpty(q)) {
				req.setQ(q);
			}
			if (page > 0) {
				req.setPageNo(page);
			}
			req.setFields("num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,volume");
			ItemsSearchResponse response = client.execute(req);
			if (!response.isSuccess()) {
				return CollectionUtil.newArrayList();
			}
			ItemSearch itemSearch = response.getItemSearch();
			if (itemSearch == null) {
				return CollectionUtil.newArrayList();
			}
			return itemSearch.getItems();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public SearchResult<TradeRate> searchTraderates(long itemId, String sellerNick, long page, long pageSize) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			TraderatesSearchRequest req = new TraderatesSearchRequest();
			req.setNumIid(itemId);
			req.setPageNo(page);
			req.setPageSize(pageSize);
			req.setSellerNick(sellerNick);
			TraderatesSearchResponse response = client.execute(req);
			if (!response.isSuccess()) {
				return new SearchResult<TradeRate>();
			}
			return new SearchResult<TradeRate>(response.getTradeRates(), response.getTotalResults());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

}
